var statusCode = {
    ok : 200,
    error : 300,
    timeout : 301
};
var ajaxdo = function() {
    $("a[target=ajaxdo]").each(function() {
        $(this).click(function(event) {
            var $this = $(this);
            var rel = $this.attr("rel");
            var param = $this.attr("param");
            var href = $this.attr("href");
            ajaxTodo(href, param, rel);
            event.preventDefault();
        });
    });
}
function ajaxTodo(url, param, callback) {
    var $callback = callback || AjaxDone;
    param = param || {};
    if (! $.isFunction($callback))
        $callback = eval('(' + callback + ')');
    $.ajax({
        type : 'POST',
        url : url,
        data : param,
        dataType : "json",
        cache : false,
        success : $callback,
        error : ajaxError
    });
}

function ajaxError(xhr, ajaxOptions, thrownError) {
    alert("Http status: " + xhr.status + " " + xhr.statusText + "\najaxOptions: " + ajaxOptions + "\nthrownError:" + thrownError + "\n" + xhr.responseText);
}

/**
 * AjaxDone是预定义的表单提交回调函数．
 *
 * {"statusCode":"200", "message":"操作成功","forwardUrl":"重新载入的页面"}
 * {"statusCode":"300", "message":"操作失败"}
 * {"statusCode":"301", "message":"会话超时"}
 *
 */
function AjaxDone(json) {
    if (json.statusCode == statusCode.error) {
        alert(json.message);
    } else if (json.statusCode == statusCode.timeout) {
        alert("sessionTimout");
    } else {
        alert(json.message);
    }
    ;
}

var dialog = function() {
    $("a[target=dialog]").each(function() {
        $(this).click(function(event) {
            var $this = $(this);
            var rel = $this.attr("rel");
            var title = $this.attr("title");
            var param = $this.attr("param");
            var href = $this.attr("href");
            dialogTodo(href, param, rel, title);
            event.preventDefault();
        });
    });
}
function dialogTodo(url, param, rel, title) {
    param = param || {};
    var rel = rel;
    var title = title;
    if (url.substr(0, 1) == '#') {
        var html = $(url).html();
        opendialog(html, rel, title);
    } else {
        $.ajax({
            type : 'POST',
            url : url,
            data : param,
            dataType : "html",
            cache : false,
            success : function(data) {
                opendialog(data, rel, title);
            },
            error : ajaxError
        });
    }
}

function opendialog(html, rel, title,close,data) {
    var p = '<div class="modal fade" id="' + rel + '" tabindex="-1" role="dialog" aria-hidden="true" aria-labelledby="myModalLabel">';
    p += '<div class="modal-dialog" role="document">';
    p += '<div class="modal-content">';
    p += '<div class="modal-header">';
    p += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
    p += '<h4 class="modal-title" id="myModalLabel">' + title + '</h4>';
    p += '</div>';
    p += '<div class="modal-body">';
    p +=  html;
    p += '</div>';
    if(close!=''){
        p += '<div class="modal-footer">';
        p += '<button type="button" class="btn btn-default" data-dismiss="modal">'+close+'</button>';
        p += '</div>';
    }
    p+='</div></div></div>';
    if(!data){
        data={};
    }
    $('body').append(p);
    $('#' + rel).modal(data);
}

//接管表单提交
function validateCallback(form, callback, confirmMsg) {
    var $form = $(form);
    callback = callback || dialogAjaxDone;
    var _submitFn = function() {
        $.ajax({
            type : form.method || 'POST',
            url : $form.attr("action"),
            data : $form.serializeArray(),
            dataType : "json",
            cache : false,
            success : callback || dialogAjaxDone,
            error : ajaxError
        });
    }
    _submitFn();
    return false;
}

/**
 * dialog上的表单提交回调函数
 * 服务器转回navTabId，可以重新载入指定的navTab. statusCode=DWZ.statusCode.ok表示操作成功, 自动关闭当前dialog
 *
 * form提交后返回json数据结构,json格式和navTabAjaxDone一致
 */
function dialogAjaxDone(json) {
    if (json.statusCode == statusCode.ok) {
        if ("close" == json.callbackType) {
            $('#' + json.rel).modal('hide');
        }
    }
}

//input 自动选择
var autoinput = function(o) {
    var o = $.extend({
        noselect : "",
        select : ""
    }, o || {});

    $('input' + o.select).each(function() {

        var $this = $(this);
        if ($this.data("autoinput")) {
            return;
        }
        $this.data("autoinput", autoinput);
        if ($this.batchhasclass(o.noselect)) {
            return;
        }
        autofocus($this);

    });

    $('body').find('textarea' + o.select).each(function() {
        var $this = $(this);
        if ($this.data("autoinput")) {
            return;
        }
        $this.data("autoinput", autoinput);
        if ($this.batchhasclass(o.noselect)) {
            return;
        }
        autofocus($this);
    });
}
function autofocus($this) {
    $this.mouseover(function() {
        $this.focus().select();
        $this.addClass('focus');
    });
    $this.blur(function() {
        $this.removeClass('focus');
    });
}

